﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Avaliacao
{
    class TowerManager
    {
        protected List<Tower> towers;
        protected List<Tower> menuTowers;

        protected const float TILE = 50f;

        public TowerManager(ContentManager Content, GameWindow Window)
        {
            towers = new List<Tower>();
            menuTowers = new List<Tower>();

            //Torres de menu
            this.menuTowers.Add(new Tower(Content.Load<Texture2D>("Towers/tower1"), Window, new Vector2(700, 200), 10, 1, 50, true));
            this.menuTowers.Add(new Tower(Content.Load<Texture2D>("Towers/tower2"), Window, new Vector2(750, 200), 10, 1, 50, true));
        }

        public Tower Update(GameTime gameTime, KeyboardState keyboard, KeyboardState last_keyboard, MouseState mouse, MouseState last_mouse)
        {
            Tower hoverTower = null; //Torre na qual o mouse se encontra em cima.
            int mouseColumn = (int)Math.Floor(mouse.X / TILE);
            int mouseRow = (int)Math.Floor(mouse.Y / TILE);

            foreach (Tower tower in towers)
            {
                tower.Update(gameTime, keyboard, last_keyboard, mouse, last_mouse);
            }

            foreach (Tower tower in menuTowers)
            {
                if (mouseColumn * TILE == tower.Position.X && mouseRow * TILE == tower.Position.Y)
                {
                    tower.setAnimation((int)Tower.animIndexes.Hover);
                    hoverTower = tower;
                }
                else
                {
                    tower.setAnimation((int)Tower.animIndexes.Still);
                }
            }

            return hoverTower;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (Tower tower in this.towers)
            {
                tower.Draw(gameTime, spriteBatch, tower.getAnimation());
            }

            foreach (Tower tower in this.menuTowers)
            {
                tower.Draw(gameTime, spriteBatch, tower.getAnimation());
            }
        }

        public void addTower(Tower tower)
        {
            towers.Add(tower);
        }
    }
}
